﻿using UnityEngine;
using System.Collections;
using WoogiUI;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
using LuaFramework;

namespace WoogiUI
{
    [RequireComponent(typeof(Button))]
    //public class WoogiButton : WoogiImage, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler, IPointerEnterHandler, IPointerClickHandler
    public class WoogiButton : WoogiImage, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler, IPointerEnterHandler, IBeginDragHandler, IDragHandler, IEndDragHandler
    {
        public MonoBehaviour mReceiveObj;
        public string mFunction;
        public string mButtonPath;
        public bool isPlayClickSound = true;
        private ButtonEvent onMouseDown;
        private ButtonEvent onMouseUp;
        private ButtonEvent onMouseEnter;
        private ButtonEvent onMouseExit;
        private ButtonEvent onMousePress;
        private ButtonEvent onStartDrag;
        private ButtonEvent onDrag;
        private ButtonEvent onEndDrag;
        private ButtonEvent onClick;
        private bool isPress = false;

        public Button button
        {
            get
            {
                Button b = GetComponent<Button>();
                if (b == null)
                {
                    b = gameObject.AddComponent<Button>();
                }
                return b;
            }
        }

        public void Awake()
        {
            button.onClick.AddListener(OnClick);
        }

        void OnClick()
        {
            if (onClick != null)
                onClick(gameObject);
            if (isPlayClickSound)
            {

            }
        }

        public void OnPointerClick(PointerEventData eventData)
        {
            if (onClick != null)
                onClick(gameObject);
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            if (onMouseDown != null)
                onMouseDown(gameObject);
            isPress = true;
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            if (onMouseUp != null)
                onMouseUp(gameObject);
            isPress = false;
        }

        public void OnPointerExit(PointerEventData eventData)
        {
            if (onMouseExit != null)
                onMouseExit(gameObject);
            isPress = false;
        }

        public void OnPointerEnter(PointerEventData eventData)
        {
            if (onMouseEnter != null)
                onMouseEnter(gameObject);
        }


        public void OnBeginDrag(PointerEventData eventData)
        {
            if (onStartDrag != null)
                onStartDrag(gameObject);
        }


        public void OnDrag(PointerEventData eventData)
        {
            if (onDrag != null)
                onDrag(gameObject);
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            if (onEndDrag != null)
                onEndDrag(gameObject);
        }


        /// <summary>
        /// set button event
        /// </summary>
        /// <param name="type">event type</param>
        /// <param name="_ButtonEvent">remove event when callback method is null,or else add event</param>
        public void SetEvent(MonoBehaviour recveiObj, UIEventType type, ButtonEvent _ButtonEvent = null)
        {
            mFunction = _ButtonEvent.Method.Name;
            mReceiveObj = recveiObj;

            switch (type)
            {
                case UIEventType.mouseUp: onMouseUp = _ButtonEvent; break;
                case UIEventType.mouseDown: onMouseDown = _ButtonEvent; break;
                case UIEventType.mouseEnter: onMouseEnter = _ButtonEvent; break;
                case UIEventType.mouseExit: onMouseExit = _ButtonEvent; break;
                case UIEventType.mouseClick: onClick = _ButtonEvent; break;
                case UIEventType.mousePress: onMousePress = _ButtonEvent; break;
                case UIEventType.mouseStartDrag: onStartDrag = _ButtonEvent; break;
                case UIEventType.mouseDrag: onDrag = _ButtonEvent; break;
                case UIEventType.mouseEndDrag: onEndDrag = _ButtonEvent; break;
            }
        }

        public void SetButtonInteractable(bool value)
        {
            if (button.interactable != value)
                button.interactable = value;
        }

        void Update()
        {
            if (isPress && onMousePress != null)
            {
                onMousePress(gameObject);
            }
        }
    }
}
